//
//  HealthSetViewController.m
//  project
//
//  Created by 圆圆陈 on 2017/12/16.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "HealthSetViewController.h"
#import "HealthSetView.h"
#import "HealthTimeSetViewController.h"
@interface HealthSetViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) UITableView    *tableView;
@property(nonatomic, strong) NSArray        *titleArr;
@property(nonatomic, strong) NSMutableArray *timeArr;
@end

@implementation HealthSetViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (SN_USER.drinkinterval != 0) {
        NSString *drinkInterval = [NSString stringWithFormat:@"%ld", (SN_USER.drinkinterval/30-1)];
        if ([drinkInterval isEqualToString:@"0"]) {
            drinkInterval = @"0.5";
        }
        
//        NSString *drinkStr = [NSString stringWithFormat:@"%@(%02ld:%02ld-%02ld:%02ld)", [NSString stringWithFormat:SN_Text(@"每%@小时提醒一次"), drinkInterval], SN_USER.drinkStartHour1, SN_USER.drinkStartMins1, SN_USER.drinkEndHour1, SN_USER.drinkEndMins1];
        NSString*drinkStr = [NSString stringWithFormat:@"%@%@",[NSString stringWithFormat:SN_Text(@"每%@小时提醒一次"), drinkInterval],SN_USER.showDrinkTime];
        [_timeArr replaceObjectAtIndex:0 withObject:drinkStr];
    }
    if (SN_USER.sedentaryinterval != 0) {
        NSString *sedentaryInterval = [NSString stringWithFormat:@"%ld", (SN_USER.sedentaryinterval/30-1)];
        if ([sedentaryInterval isEqualToString:@"0"]) {
            sedentaryInterval = @"0.5";
        }
//        NSString *scheduleStr = [NSString stringWithFormat:@"%@(%02ld:%02ld-%02ld:%02ld)", [NSString stringWithFormat:SN_Text(@"每%@小时提醒一次"), sedentaryInterval], SN_USER.sedentaryStartHour1, SN_USER.sedentaryStartMins1, SN_USER.sedentaryEndHour1, SN_USER.sedentaryEndMins1];
        NSString*scheduleStr = [NSString stringWithFormat:@"%@%@",[NSString stringWithFormat:SN_Text(@"每%@小时提醒一次"), sedentaryInterval],SN_USER.showSedentaryTime];
        [_timeArr replaceObjectAtIndex:1 withObject:scheduleStr];
    }
    [_tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *StateView = [[UIView alloc] init];
    StateView.frame           = CGRectMake(0, 0, kSCREEN_WIDTH, 20);
    StateView.backgroundColor = BBIGSTATECOLOR;
    [self.view addSubview:StateView];
    
    UIView *naView = [[UIView alloc] init];
    naView.frame           = CGRectMake(0, 20+FitSimpleIpx(0, 20), kSCREEN_WIDTH, 44);
    naView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:naView];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20), screenWidth, 44)];
    lable.textColor     = [UIColor blackColor];
    lable.font          = SNFONT(17);
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text          = HHH_Text(@"健康提醒");
    
    [self.view addSubview:lable];
    
    UIButton *leftButton = [[UIButton alloc] init];
    leftButton.frame = CGRectMake(5, 20+FitSimpleIpx(0, 20), 44, 44);
    [leftButton setImageNormal:@"进入"];
    [leftButton addTarget:self action:@selector(leftbuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    _tableView                 = [[UITableView alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20)+44+2, HHHWIDTH, HHHHEIGHT-20-FitSimpleIpx(0, 20)-44-2) style:UITableViewStylePlain];
    _tableView.delegate        = self;
    _tableView.dataSource      = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _titleArr = @[HHH_Text(@"喝水提醒"), HHH_Text(@"久坐提醒")];
    _timeArr  = [NSMutableArray arrayWithObjects:@"", @"", nil];
    
    UIImageView *shadowLine = [[UIImageView alloc] init];
    shadowLine.frame = SNFRAME(0, 20+FitSimpleIpx(0, 20) + 44 -2, HHHWIDTH, 2);
    shadowLine.image = [UIImage image:@"lineImage"];
    [self.view addSubview:shadowLine];
}

- (void)leftbuttonClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return FitH(60);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell                           = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.selectionStyle            = UITableViewCellSelectionStyleNone;
        cell.detailTextLabel.textColor = UIColorHEX(0x727272);
    }
    cell.textLabel.text = _titleArr[indexPath.row];
    cell.textLabel.font = SNFONT(15);
    
    UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    setBtn.frame = CGRectMake(FitW(334)-FitW(50), FitH(5), FitW(50), FitH(60)-FitH(10));
    [setBtn setImageNormal:@"设置1"];
    setBtn.tag = 100+indexPath.row;
    [setBtn addTouchUpTarget:self action:@selector(setBtnClick:)];
    [cell addSubview:setBtn];
    
    ZJSwitch *btn = [[ZJSwitch alloc] initWithFrame:CGRectMake(HHHWIDTH-50, FitH(10), 50, 22)];
    btn.tag = 100 +indexPath.row;
    if (indexPath.row == 0) {
        btn.on = SN_USER.drinkOpen;
    } else {
        btn.on = SN_USER.sedentaryOpen;
    }
    [btn addTarget:self action:@selector(handleSwitchEvent:) forControlEvents:UIControlEventValueChanged];
    cell.accessoryView = btn;
    
    cell.detailTextLabel.font = SNFONT(11);
    cell.detailTextLabel.text = _timeArr[indexPath.row];
   
    return cell;
}

- (void)setBtnClick:(UIButton *)sender {
    HealthTimeSetViewController *vc = [[HealthTimeSetViewController alloc]init];
    
    vc.type = sender.tag-100;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)handleSwitchEvent:(ZJSwitch *)sender {
    if (sender.tag == 100) {
        SN_USER.drinkOpen = sender.on;
        [SN_EveryDayModelHelp startDrinkWater];
    } else {
        SN_USER.sedentaryOpen = sender.on;
        [SN_EveryDayModelHelp startsedentary];
    }
    [SN_USER updateToDB];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

